import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '../views/Index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
	redirect:'/index'
  },
  {
    path: '/index',
    name: 'Index',
    component: Index
  },
  {
    path: '/movieinfo',
    name: 'MovieInfo',
    component: () => import('../views/MovieInfo.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
{
    path: '/admin',
    name: 'admin',
    component: () => import('../views/admin/Admin'),
    children:[
      {
        path: 'list-movie',
        name: 'movielist',
        component: () => import('../views/admin/movies/movielist')
      },{
        path: 'create-movie',
        name: 'moviecreate',
        component: () => import('../views/admin/movies/moviecreate')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{
  //如果请求后台管理功能的路由
  if(to.fullPath.indexOf("/admin")>=0){
    //判断是否登录，localStorage存在usertoken
    let usertoken=localStorage.getItem("usertoken");
    if(usertoken!=null){
      next();
    }else{
      next({path:'/login'});
    }
  }
  next();
})
export default router